package com.kangye.game.controller;

import com.alibaba.fastjson.JSON;
import com.kangye.game.pojo.TUser;
import com.kangye.game.service.UserService;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: GameProject
 * @description:
 * @author: szq
 * @create: 2021-09-02 10:43
 **/
@RestController
@CrossOrigin
public class UserController {

    @Resource
    private UserService userService;

    @RequestMapping("findAllUser.action")
    public Object findAllList(int num, int size) {
        //分页 当前页和每页条数
        List<TUser> lists = userService.findAllList(num, size);
        //总条数
        int zongtiaoshu = userService.findCount();
        //总页数
        int zongpage = 0;
        //算出总页数
        if (zongtiaoshu % size == 0) {
            zongpage = zongtiaoshu / size;
        } else {
            zongpage = zongtiaoshu / size + 1;
        }
        //放到map集合
        Map<String, Object> map = new HashMap<>();
        map.put("user", JSON.toJSON(lists));
        map.put("num", num);
        map.put("size", size);
        map.put("zongpage", zongpage);
        return map;
    }

    @RequestMapping("addUser.action")
    public String addUser(TUser user) {
        int a = userService.addUser(user);
        if (a != 0) {
            return "200";
        } else {
            return "400";
        }
    }

    @RequestMapping("findOne.action")
    public Map<String, Object> findOne(int id) {
        TUser ids = userService.findOne(id);
        Map<String, Object> map = new HashMap<>();
        map.put("id", JSON.toJSON(ids));
        return map;
    }

    @RequestMapping("updUser.action")
    public String updUser(TUser user) {
        int u = userService.updUser(user);
        if (u != 0) {
            return "200";
        } else {
            return "400";
        }

    }

    @RequestMapping("delUser.action")
    public String delUser(int id) {
        userService.delUser(id);
        return "删除成功！";
    }

    @RequestMapping("findLike.action")
    public Object findLike(String username) {
        List<TUser> user = userService.findLike(username);
        Map<String, Object> map = new HashMap<>();
        map.put("username", JSON.toJSON(user));
        return map;
    }
}